package com.meichis.yndms.repository.manager;

import android.util.ArrayMap;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.encrypt.AESProvider;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.FeedBack;
import com.meichis.yndms.repository.ApiUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TerminalFeedbackManagert extends BaseManager {

    private static TerminalFeedbackManagert INSTANCE;

    public static TerminalFeedbackManagert getInstance() {
        if (INSTANCE == null) {
            synchronized (TerminalFeedbackManagert.class) {
                if (INSTANCE == null) {
                    INSTANCE = new TerminalFeedbackManagert();
                }
            }
        }
        return INSTANCE;
    }

    public void GetMyFeedbackList(final MutableLiveData<Boolean> showDialog, String partnerId, int state, String startDate, String endDate, MutableLiveData<List<FeedBack>> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("PartnerId", partnerId);
        hs.put("State", state);
        hs.put("BeginDate", startDate);
        hs.put("EndDate", endDate);
        getRootApi().GetMyFeedbackList(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<FeedBack>>() {
                }.getType(), new IWebServiceCallback<List<FeedBack>>() {
                    @Override
                    public void onSucceed(int sequence, List<FeedBack> resultPack, String ReturnInfo, int Return) {
                        if (resultPack == null || resultPack.size() == 0) {
                            MessageTools.showLongToast("暂无数据！");
                            data.setValue(new ArrayList<>());
                        } else {
                            data.setValue(resultPack);
                        }
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));
    }

    public void GetMyFeedbackByID(final MutableLiveData<Boolean> showDialog, int ID, MutableLiveData<FeedBack> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("ID", ID);
        getRootApi().GetMyFeedbackByID(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<FeedBack>() {
                }.getType(), new IWebServiceCallback<FeedBack>() {
                    @Override
                    public void onSucceed(int sequence, FeedBack resultPack, String ReturnInfo, int Return) {
                        showDialog.setValue(false);
                        data.setValue(resultPack);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        MessageTools.showShortToast(error);
                        showDialog.setValue(false);
                    }
                }));
    }


    public void SaveFeedbackReply(final MutableLiveData<Boolean> showDialog, String PartnerId, String StaffId, int FeedbackID, String Content, String GUIDs, MutableLiveData<Boolean> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("PartnerId", PartnerId);
        hs.put("StaffId", StaffId);
        hs.put("FeedbackID", FeedbackID);
        hs.put("Content", Content);
        hs.put("GUIDs", GUIDs);
        getRootApi().SaveFeedbackReply(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<Object>() {
                }.getType(), new IWebServiceCallback<Object>() {
                    @Override
                    public void onSucceed(int sequence, Object resultPack, String ReturnInfo, int Return) {
                        showDialog.setValue(false);
                        data.setValue(true);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        MessageTools.showShortToast(error);
                        showDialog.setValue(false);
                        data.setValue(false);
                    }
                }));
    }

    public void SetFeedbackFinish(final MutableLiveData<Boolean> showDialog, String PartnerId, String StaffId, int FeedbackID, MutableLiveData<Boolean> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("PartnerId", PartnerId);
        hs.put("StaffId", StaffId);
        hs.put("FeedbackID", FeedbackID);
        getRootApi().SetFeedbackFinish(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<Object>() {
                }.getType(), new IWebServiceCallback<Object>() {
                    @Override
                    public void onSucceed(int sequence, Object resultPack, String ReturnInfo, int Return) {
                        showDialog.setValue(false);
                        data.setValue(true);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        MessageTools.showShortToast(error);
                        showDialog.setValue(false);
                        data.setValue(false);
                    }
                }));
    }

}
